//
//  WJTopicCell.m
//  BeDeJie
//
//  Created by jerry on 2017/2/1.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "WJTopicCell.h"
#import <UIImageView+WebCache.h>
#import "WJTopic.h"
#import "WJTopicVideoView.h"
#import "WJTopicVoiceView.h"
#import "WJTopicPictureView.h"

@interface WJTopicCell()
@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *passtimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *msgLabel;
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
@property (weak, nonatomic) IBOutlet UIButton *shareButton;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;

@property (weak, nonatomic) IBOutlet UIView *hotCommentView;
@property (weak, nonatomic) IBOutlet UILabel *hotCommentTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *hotCommentContentLabel;

/* 中间控件 */
/** 图片控件 */
@property (nonatomic, weak) WJTopicPictureView *pictureView;
/** 声音控件 */
@property (nonatomic, weak) WJTopicVoiceView *voiceView;
/** 视频控件 */
@property (nonatomic, weak) WJTopicVideoView *videoView;

@end

@implementation WJTopicCell

#pragma mark - 懒加载
- (WJTopicPictureView *)pictureView {
    if (!_pictureView) {
        WJTopicPictureView *pictureView = [WJTopicPictureView viewFromXib];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView;
    }
    return _pictureView;
}

- (WJTopicVoiceView *)voiceView {
    if (!_voiceView) {
        WJTopicVoiceView *voiceView = [WJTopicVoiceView viewFromXib];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
    }
    return _voiceView;
}

- (WJTopicVideoView *)videoView {
    if (!_videoView) {
        WJTopicVideoView *videoView = [WJTopicVideoView viewFromXib];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
    }
    return _videoView;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage resizableImage:@"cell_bg"]];
}

- (void)setFrame:(CGRect)frame
{
//    frame.origin.x += WJMarin;
//    frame.size.width -= 2 * WJMarin;
//    frame.size.height -= WJMarin;
    
    [super setFrame:frame];
}

-(void)setTopic:(WJTopic *)topic {
    _topic = topic;
    
    // 顶部控件的数据
    UIImage *placeholderImage = [UIImage circleImageWithName:@"icon_user_default"];
    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:topic.profile_image] placeholderImage:placeholderImage options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        // 图片下载失败，直接返回，按照它的默认做法
        if (!image) return;
        
        self.profileImageView.image = [UIImage circleImageWithImage:image];
    }];
    self.nameLabel.text = topic.name;
    self.passtimeLabel.text = topic.passtime;
    self.msgLabel.text = topic.text;
    
    // 最热评论
    if (topic.top_cmt.count) { // 有最热评论
        self.hotCommentView.hidden = NO;
        
        NSDictionary *cmt = topic.top_cmt.firstObject;
        NSString *content = cmt[@"content"];
        if (content.length == 0) { // 语音评论
            content = @"[语音评论]";
        }
        NSString *username = cmt[@"user"][@"username"];
        self.hotCommentContentLabel.text = [NSString stringWithFormat:@"%@ : %@", username, content];
    } else { // 没有最热评论
        self.hotCommentView.hidden = YES;
    }
    
    // 中间的内容
    if (topic.type == WJTopicTypePicture) { // 图片
        self.pictureView.hidden = NO;
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
        //把值传给picture自定义控件中
        self.pictureView.topic = topic;
    } else if (topic.type == WJTopicTypeVoice) { // 声音
        self.pictureView.hidden = YES;
        self.voiceView.hidden = NO;
        self.videoView.hidden = YES;
        //把值传给voice自定义控件中
        self.voiceView.topic = topic;
    } else if (topic.type == WJTopicTypeVideo) { // 视频
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
        self.videoView.hidden = NO;
        //把值传给video自定义控件中
        self.videoView.topic = topic;
    } else if (topic.type == WJTopicTypeWord) { // 段子
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    }

    // 底部按钮的文字
    [self setupButtonTitle:self.dingButton number:topic.ding placeholder:@"顶"];
    [self setupButtonTitle:self.caiButton number:topic.cai placeholder:@"踩"];
    [self setupButtonTitle:self.shareButton number:topic.repost placeholder:@"分享"];
    [self setupButtonTitle:self.commentButton number:topic.comment placeholder:@"评论"];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    if (self.topic.type == WJTopicTypePicture) { // 图片
        self.pictureView.frame = self.topic.middleFrame;
    } else if (self.topic.type == WJTopicTypeVoice) { // 声音
        self.voiceView.frame = self.topic.middleFrame;
    } else if (self.topic.type == WJTopicTypeVideo) { // 视频
        self.videoView.frame = self.topic.middleFrame;
    }
}

/**
 *  设置按钮文字
 *  @param number      按钮的数字
 *  @param placeholder 数字为0时显示的文字
 */
- (void)setupButtonTitle:(UIButton *)button number:(NSInteger)number placeholder:(NSString *)placeholder
{
    if (number >= 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万", number / 10000.0] forState:UIControlStateNormal];
    } else if (number > 0) {
        [button setTitle:[NSString stringWithFormat:@"%zd", number] forState:UIControlStateNormal];
    } else {
        [button setTitle:placeholder forState:UIControlStateNormal];
    }
}

@end
